/* ==============================================================================
 * 创 建 者：lyt
 * 创建日期：2017-07-14	
 * 功能描述：

 * 修 改 人:
 * 修改时间:
 * 修改描述：
 * ==============================================================================*/

//会员图谱（接点图谱）
var getLinkUrl = baseUrl + 'MemberF/GetReferralLink';
//会员信息获取
var getMemberViewUrl = baseUrl + 'MemberF/MemberView';

var vu,clipboard;
$(function(){
	initVue();
	$("#saveImg").click(function(){
		$("#saveImg").hide();
		html2canvas(document.querySelector(".mui-content")).then(canvas => {
//			document.body.appendChild(canvas)
			var img = document.createElement('img');
		    img.src = canvas.toDataURL();
//		    document.body.appendChild(img);
			$(".mui-content .imgBox").append(img);
			$(".mui-content .imgBox").show();
			$("#saveImg").show();
//		    var dataUrl = canvas.toDataURL();
//		    download(dataUrl);
		});
	});  
});

function initVue(){
	vu = new Vue({
		el : '#mescroll',
		data : { 
			member : '',
			ReferralLink :'',
			showBtn : true,
			showImg : false,
			ReferrCode:'',
		},
		mounted: function() {
//			this.getList();
			this.getMemberView();
			this.getLink();
		},
		methods : {
			getCopy:getCopy,
			getMemberView : getMemberView,
			getLink : getLink,
			getCode:function(){
				var _this = this;
				jQuery('#code').empty();
//			    $('#code').qrcode(_this.ReferralLink); //任意字符串
				jQuery('#code').qrcode({
		     	 render    : "canvas",
		         text    : _this.ReferralLink,
		         width : "260",               //二维码的宽度
                 height : "260",              //二维码的高度
//               background : "#ffffff",       //二维码的后景色
//               foreground : "#000000",        //二维码的前景色
//               src: 'phoneContent/img/logo.png'             //二维码中间的图片
		     });
		     //二维码变图片
		      var mycanvas1=document.getElementsByTagName('canvas')[0];
			  var img=convertCanvasToImage(mycanvas1);
				jQuery('#code').empty();
				$('#code').append(img);
			
			}
		}
	})
}

function getCopy(id){
	clipboard = new ClipboardJS('.copy',{
		text: function() {
			if(id==1){
				return vu.ReferralLink;
			}
			if(id==2){
				return vu.ReferrCode;
			}
        }
	});
	clipboard.on('success', function(e) {
        mui.toast('复制成功！');
    });

    clipboard.on('error', function(e) {
        mui.toast('复制失败！');
    });
}

//保存图片到手机
function savePic(){         
    var picurl= $("#code img").attr("src");
    //alert(picurl);
    savePicture(picurl);
}

var triggerEvent = "touchstart";
function savePicture(Url){
    var blob=new Blob([''], {type:'application/octet-stream'});
    var url = URL.createObjectURL(blob);
    var a = document.createElement('a');
    a.href = Url;
    a.download = Url.replace(/(.*\/)*([^.]+.*)/ig,"$2").split("?")[0];
    var e = document.createEvent('MouseEvents');
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
    URL.revokeObjectURL(url);
}

function download(src) {
    var $a = document.createElement('a');
    $a.setAttribute("href", src);
    $a.setAttribute("download", "code.jpg");

    var evObj = document.createEvent('MouseEvents');
    evObj.initMouseEvent( 'click', true, true, window, 0, 0, 0, 0, 0, false, false, true, false, 0, null);
    $a.dispatchEvent(evObj);
    vu.showBtn = true;
};

function getLink(){
	common.get(getLinkUrl,{IsPhone : true},'json',function(r){
		if(r.ErrorCode==0){
			if (r.ErrorCode == 0) {
				vu.ReferralLink = r.Data.qRCodeUrl;
				vu.ReferrCode = r.Data.ReferrCode;
	           	vu.getCode();
			}else{
				mui.toast(r.ErrorMsg);
			}
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}



function getMemberView(){
	common.get(getMemberViewUrl,{},'json',function(r){
		if(r.ErrorCode==0){
			vu.member = r.Data.member;
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}


